package com.lajiaomian.springbootruijiwaimai.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lajiaomian.springbootruijiwaimai.dto.DishDto;
import com.lajiaomian.springbootruijiwaimai.mapper.CategoryMapper;
import com.lajiaomian.springbootruijiwaimai.mapper.DishMapper;
import com.lajiaomian.springbootruijiwaimai.pojo.Category;
import com.lajiaomian.springbootruijiwaimai.pojo.Dish;
import com.lajiaomian.springbootruijiwaimai.pojo.DishFlavor;
import com.lajiaomian.springbootruijiwaimai.service.impl.CategoryService;
import com.lajiaomian.springbootruijiwaimai.service.impl.DishFlavorService;
import com.lajiaomian.springbootruijiwaimai.service.impl.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author lajiaomian
 * @create 2022-08-13-10:27
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public Long CountCategoryId(Long categoryId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id",categoryId);
        return count(wrapper);
    }

    @Transactional
    @Override
    public void saveDishDto(DishDto dishDto) {
        //将菜品信息保存到dish表
        save(dishDto);
        //将菜品信息保存到DishFlavor表
        //注意：保存的信息只会有name和value，缺少id，需要自行添加
        Long dishId = dishDto.getId();
        //此处可以使用foreach和stream方式，后者在数据多的时候更有效率
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor: flavors) {
            dishFlavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getDishById(Long id) {
        Dish dish = this.getById(id);
        Long dishId = dish.getId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dish_id",dishId);
        List list = dishFlavorService.list(wrapper);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public void updateDishDto(DishDto dishDto) {
        //先更新dish表
        this.updateById(dishDto);
        //删除dishFlavor表的相关信息
        Long dishId = dishDto.getId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("dish_id",dishId);
        dishFlavorService.remove(wrapper);
        //重新插入
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor: flavors) {
            dishFlavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }


}
